package com.niodata.dp.core.usermgr.dao;

import com.niodata.dp.core.usermgr.enums.AuthType;
import com.niodata.dp.core.usermgr.model.ResAuth;
import com.niodata.dp.core.usermgr.model.result.ResAuthUserInfo;
import java.util.List;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

/**
 * Created by ndp on 17-3-8.
 */
@Mapper
public interface ResAuthMapper {

  @Insert("insert into RES_AUTH (GROUP_ID,AUTH_TYPE,AUTH_PATH,AUTH_USERID,OPERATE_TYPE) values "
        + "(#{resAuth.groupId},#{resAuth.authType},#{resAuth.authPath},#{resAuth.authUserId},"
        + "#{resAuth.operateType})")
  void insert(@Param("resAuth") ResAuth resAuth);

  @Delete("delete from RES_AUTH where GROUP_ID=#{resAuth.groupId} and AUTH_TYPE=#{resAuth.authType}"
        + " and AUTH_PATH=#{resAuth.authPath} and AUTH_USERID=#{resAuth.authUserId}")
  void deleteResAuth(@Param("resAuth") ResAuth resAuth);

  @Delete("delete from RES_AUTH where GROUP_ID=#{groupId} and AUTH_TYPE=#{authType}"
        + " and AUTH_PATH=#{authPath} and AUTH_USERID=#{authUserId}")
  int delete(@Param("groupId") Long groupId, @Param("authUserId") Long authUserId,
        @Param("authType") AuthType authType,
        @Param("authPath") String authPath);

  @Delete("delete from RES_AUTH where GROUP_ID=#{groupId} and AUTH_TYPE=#{authType}"
        + " and AUTH_PATH=#{authPath}")
  void deleteByGroup(@Param("groupId") Long groupId,
        @Param("authType") AuthType authType,
        @Param("authPath") String authPath);

  @Update("update RES_AUTH set OPERATE_TYPE=#{resAuth.operateType} where"
        + " GROUP_ID=#{resAuth.groupId} and AUTH_USERID=#{resAuth.authUserId} "
        + "and AUTH_TYPE=#{resAuth.authType} and AUTH_PATH=#{resAuth.authPath}")
  int update(@Param("resAuth") ResAuth resAuth);

  @Select("select * from RES_AUTH where AUTH_USERID=#{authUserId} and GROUP_ID=#{groupId} and "
        + "AUTH_TYPE=#{authType} and AUTH_PATH=#{authPath}")
  @ResultMap("resAuthResult")
  ResAuth checkExistsResAuth(@Param("authUserId") Long authUserid, @Param("groupId") Long groupId,
        @Param("authType") AuthType authType, @Param("authPath") String authPath);

  List<ResAuth> getResAuth(@Param("authUserId") Long authUserId, @Param("groupIds") Long[] groupIds,
        @Param("authType") AuthType authType, @Param("authPath") String authPath);

  @Select("select DISTINCT r.AUTH_TYPE as authType,r.AUTH_PATH as authPath,"
        + "r.AUTH_USERID as authUserId,r.OPERATE_TYPE as operateType,r.CREATE_TIME as createTime,"
        + "(CASE WHEN u.USER_NAME IS NULL "
        + "THEN CONVERT(AUTH_USERID,CHAR(12)) ELSE u.USER_NAME END) AS userName "
        + "FROM RES_AUTH AS r LEFT JOIN USER_INFO AS u ON r.AUTH_USERID=u.USER_ID "
        + "where r.GROUP_ID=#{groupId}")
  List<ResAuthUserInfo> getResAuthListByGroupId(@Param("groupId") Long groupId);

  @Update("update RES_AUTH set AUTH_USERID=#{newId} "
        + "where GROUP_ID=#{groupId} and AUTH_USERID=#{oldId}")
  void updateMasterId(@Param("groupId") Long groupId, @Param("oldId") Long oldId,
        @Param("newId") Long newId);

}
